package com.xxxx.seckill.rabbitmq;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xxxx.seckill.pojo.Order;
import com.xxxx.seckill.pojo.SeckillGoods;
import com.xxxx.seckill.pojo.SeckillMessage;
import com.xxxx.seckill.pojo.SeckillOrder;
import com.xxxx.seckill.service.IGoodsService;
import com.xxxx.seckill.service.IOrderService;
import com.xxxx.seckill.service.ISeckillGoodsService;
import com.xxxx.seckill.utils.JsonUtil;
import com.xxxx.seckill.vo.GoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * 消息消费者
 *
 * @author: LC
 * @date 2022/3/7 7:44 下午
 * @ClassName: MQReceiver
 */
@Service
@Slf4j
public class MQReceiver {

    @Autowired
    private IGoodsService itGoodsServicel;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IOrderService itOrderService;
    @Autowired
    private ISeckillGoodsService seckillGoodsService;


    /**
     * 下单操作
     *
     * @param
     * @return void
     * @author LiChao
     * @operation add
     * @date 6:48 下午 2022/3/8
     **/
    @RabbitListener(queues = "seckillQueue")
    public void receive(String message) {
        log.info("接收消息：" + message);
        SeckillMessage seckillMessage = JsonUtil.jsonStr2Object(message, SeckillMessage.class);
        Long goodsId = seckillMessage.getGoodId();
        String usersId = seckillMessage.getUsersId();
        //判断库存大小
        GoodsVo goodsVo = itGoodsServicel.findGoodsVoByGoodsId(goodsId);
        if (goodsVo.getStockCount() < 1) {
            return;
        }
        //判断是否重复抢购
        SeckillOrder tSeckillOrder = (SeckillOrder) redisTemplate.opsForValue().get("order:" + usersId + ":" + goodsId);
        if (tSeckillOrder != null) {
            return;
        }
        //下单操作
        itOrderService.seckill(usersId, goodsVo);
        SeckillGoods seckillGoods = seckillGoodsService.getOne(new QueryWrapper<SeckillGoods>().eq("goods_id", goodsVo.getId()));
        if (seckillGoods.getStockCount() <= 0) {
            ValueOperations valueOperations = redisTemplate.opsForValue();
            valueOperations.set("isStockEmpty:" + goodsVo.getId(), "0");
        }
    }

    /**
     * 处理支付逻辑
     *
     * @param message
     */
    @RabbitListener(queues = "payQueue")
    public void payService(String message) {
        log.info("支付订单：" + message);
        // 订单信息
        final SeckillOrder seckillOrder = JSON.parseObject(message, SeckillOrder.class);

        final boolean updateBoolean = itOrderService.update(
                new UpdateWrapper<Order>()
                        .eq("user_id", seckillOrder.getUserId())
                        .eq("goods_id",seckillOrder.getGoodsId())
                        .set("status", 1));

        // 判断是否更新成功
        if (updateBoolean) {
            redisTemplate.opsForValue().set("pay:" + seckillOrder.getUserId() + ":" + seckillOrder.getGoodsId(), seckillOrder);
        }
    }


    /**
     * 处理取消逻辑
     *
     * @param message
     */
    @RabbitListener(queues = "cannelQueue")
    public void cancelService(String message) {
        log.info("取消订单：" + message);
        // 订单信息
        final SeckillOrder seckillOrder = JSON.parseObject(message, SeckillOrder.class);
        itOrderService.cancelOrder(seckillOrder);
    }

}
